//
//  ZDBRedEnvelopesCustomAlertView.m
//  ZDB
//
//  Created by Kk on 2020/5/28.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBRedEnvelopesCustomAlertView.h"
#import "ZDBAlertUtils.h"
#define MainScreenRect [UIScreen mainScreen].bounds
@interface ZDBRedEnvelopesCustomAlertView()
@property (nonatomic,strong)UIView *bgView;
@property (nonatomic,strong)UIImageView *notOpenImag;
@property (nonatomic,strong)UILabel *notContentLab;

@property (nonatomic,strong)UIImageView *openImg;
@property (nonatomic,strong)UILabel *priceLab;
@property (nonatomic,strong)UILabel *titleLab;
@property (nonatomic,strong)UIButton *cancelButton;
@end
@implementation ZDBRedEnvelopesCustomAlertView

- (instancetype)initWithimage:(NSString *)imgStr;
{
    if (self=[super init]) {
          self.frame=MainScreenRect;
          self.backgroundColor=[UIColor colorWithWhite:.0 alpha:.6];
         
         [self addSubview:self.bgView];
        
         [_bgView addSubview:self.notOpenImag];
         [_notOpenImag addSubview:self.notContentLab];
        
         [_bgView addSubview:self.openImg];
         [_openImg addSubview:self.priceLab];
         [_openImg addSubview:self.titleLab];
         [_bgView addSubview:self.cancelButton];
         
          
      }
      return self;
}
- (UIView *)bgView
{
    if (!_bgView) {

        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RealValue_W(565), RealValue_W(706 +108))];
        _bgView.center =self.center;
        _bgView.backgroundColor=[UIColor colorWithWhite:.0 alpha:0];
    }
    return _bgView;
}
- (UIImageView *)notOpenImag
{
    if (!_notOpenImag) {
        _notOpenImag = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _bgView.width, RealValue_W(706))];
        _notOpenImag.image = [UIImage imageNamed:@"weidakai"];
        _notOpenImag.userInteractionEnabled = YES;
        UITapGestureRecognizer *openTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(open)];
       [_notOpenImag addGestureRecognizer:openTap];
    }
    return _notOpenImag;
}
- (UILabel *)notContentLab
{
    if (!_notContentLab) {
        _notContentLab = [[UILabel alloc] initWithFrame:CGRectMake(0, _notOpenImag.height - RealValue_W(144 +106), _notOpenImag.width, RealValue_W(106))];
        _notContentLab.textColor = ColorStr(@"#fbdfbd");
        _notContentLab.text = @"恭喜您\n获得红包奖励";
        _notContentLab.numberOfLines = 0;
        _notContentLab.font = AutoBoldFont(22);
        _notContentLab.textAlignment = NSTextAlignmentCenter;
    }
    return _notContentLab;
}
- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(_bgView.width/2 -RealValue_W(36), _notOpenImag.bottom + RealValue_W(36), RealValue_W(72), RealValue_W(72))];
        [_cancelButton setImage:[UIImage imageNamed:@"red_cancel"] forState:0];
        WeakSelf
        [_cancelButton add_BtnClickHandler:^(NSInteger tag) {
           [weakSelf dismissAlertView];
        }];
    }
    return _cancelButton;
}
- (UIImageView *)openImg
{
    if (!_openImg) {
        _openImg = [[UIImageView alloc] initWithFrame:CGRectMake(_bgView.width/2 - RealValue_W(542)/2 -12, 0, RealValue_W(542), RealValue_W(708))];
        _openImg.image = [UIImage imageNamed:@"yidakai"];
        _openImg.hidden = YES;
    }
    return _openImg;
}

- (UILabel *)priceLab
{
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:CGRectMake(RealValue_W(30), RealValue_W(190), _openImg.width, RealValue_W(66))];
        _priceLab.textColor = MAINCOLOR;
       
        _priceLab.font = AutoBoldFont(24);
        _priceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _priceLab;
}
- (UILabel *)titleLab
{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:CGRectMake(RealValue_W(30),_openImg.height - RealValue_W(188), _openImg.width, RealValue_W(80))];
        _titleLab.textColor = ColorStr(@"#fbdfbd");
        _titleLab.font = AutoBoldFont(24);
        _titleLab.attributedText = [ZBDUnit finderattributedString:@"VIP 专享红包" attributedString:@"VIP" color:ColorStr(@"#fbdfbd") font:AutoBoldFont(28)];
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}
#pragma 红包开启
- (void)open
{
    WeakSelf
     [[ZDBAlertUtils sharedZDBAlertUtils] show:kWindow];
    NSString  * banber = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getRed"];
    [[SmileHttpTool sharedInstance] POST:banber parameters:@{} origin:YES success:^(NSInteger statusCode, NSString *message, id responseObject) {
         [[ZDBAlertUtils sharedZDBAlertUtils] stopWait];
        NSDictionary * dic = responseObject;
        if (!kDictIsEmpty(dic)) {
            if ([dic[@"statusCode"] integerValue] ==0) {//已领取
                weakSelf.notOpenImag.hidden = YES;
                weakSelf.openImg.hidden = NO;
                NSString * price = kFormat(@"%@ 元", dic[@"data"]);
                weakSelf.priceLab.attributedText = [ZBDUnit finderattributedString:price attributedString:kFormat(@"%@", dic[@"data"])  color:MAINCOLOR font:AutoBoldFont(54)];
                self.userInteractionEnabled = YES;
                UITapGestureRecognizer *openTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissAlertView)];
                [self addGestureRecognizer:openTap];
                
            }
                
           
        }
        
    } failure:^(id responseObject, NSError *error) {
        
         [[ZDBAlertUtils sharedZDBAlertUtils] stopWait];
    }];
}
-(void)dismissAlertView
{
    [self removeFromSuperview];
}
-(void)showLXAlertView
{
    [kWindow addSubview:self];
    [self setShowAnimation];
}
-(void)setShowAnimation{
    WeakSelf;
    [UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [weakSelf.bgView.layer setValue:@(0) forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.23 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [weakSelf.bgView.layer setValue:@(1.2) forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.09 delay:0.02 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                [weakSelf.bgView.layer setValue:@(.9) forKeyPath:@"transform.scale"];
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.05 delay:0.02 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    [weakSelf.bgView.layer setValue:@(1.0) forKeyPath:@"transform.scale"];
                } completion:^(BOOL finished) {
                    weakSelf.userInteractionEnabled = YES;
                }];
            }];
        }];
    }];
}
@end
